/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.mrp.ppm.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import merlion.common.entity.Staff;
import merlion.common.util.Consts;

/**
 *
 * @author Zhu Xin
 */
@Entity
public class DailyMPS implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String dailyMPSIdStr;
    private Timestamp dateCreated;
    private Integer startPeriodYear;
    private Integer startPeriodMonth;
    private Integer startPeriodDate; //indicate week No.  range: 1 to 5
    @ManyToOne(targetEntity = SOP.class, cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
    private SOP sop;

    @OrderBy("productIndex")
    @OneToMany(targetEntity = IndivDMPS.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private List<IndivDMPS> indivDMPSList = new ArrayList<IndivDMPS>();

    public DailyMPS() {
    }

    public DailyMPS(int startYear, int startMonth, int startDay, Timestamp currentTime) {
        for (int i = 0; i < 4; i++) {
            // represent the 4 products
            IndivDMPS temp = new IndivDMPS();
            temp.setProductIndex(i);
            indivDMPSList.add(temp);
        }
        // set datecreated
        dateCreated = currentTime;
        startPeriodMonth = new Integer(startMonth);
        startPeriodYear = new Integer(startYear);
        startPeriodDate = new Integer(startDay);
        //dailyMPSIdStr
        dailyMPSIdStr = "DMPS" + startYear;
        if (String.valueOf(startMonth).length() < 2) {
            dailyMPSIdStr += ("0" + String.valueOf(startMonth));
        } else {
            dailyMPSIdStr += String.valueOf(startMonth);
        }
        if (String.valueOf(startDay).length() < 2) {
            dailyMPSIdStr += ("0" + String.valueOf(startDay));
        } else {
            dailyMPSIdStr += String.valueOf(startDay);
        }
    }
    
    public String getDMPSPeriod() {
        String monthStr = getMonthEnglish(startPeriodMonth);
        monthStr = monthStr + ", " + startPeriodYear;
        return monthStr;
    }
    
    private String getMonthEnglish(int i) {
        switch (i) {
            case 1:
                return Consts.JAN;
            case 2:
                return Consts.FEB;
            case 3:
                return Consts.MAR;
            case 4:
                return Consts.APR;
            case 5:
                return Consts.MAY;
            case 6:
                return Consts.JUN;
            case 7:
                return Consts.JUL;
            case 8:
                return Consts.AUG;
            case 9:
                return Consts.SEP;
            case 10:
                return Consts.OCT;
            case 11:
                return Consts.NOV;
            case 12:
                return Consts.DEC;
            default:
                return "";
        }
    }
    
    public String getFormattedCreatedDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
        return sdf.format(dateCreated);
    }

    public String getFormattedDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(dateCreated);
    }

    public IndivDMPS returnIndivDMPS(Integer i) {
        // 0-> product a
        return indivDMPSList.get(i);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDailyMPSIdStr() {
        return dailyMPSIdStr;
    }

    public void setDailyMPSIdStr(String dailyMPSIdStr) {
        this.dailyMPSIdStr = dailyMPSIdStr;
    }

    public Timestamp getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Timestamp dateCreated) {
        this.dateCreated = dateCreated;
    }

    public List<IndivDMPS> getIndivDMPSList() {
        return indivDMPSList;
    }

    public void setIndivDMPSList(List<IndivDMPS> indivDMPSList) {
        this.indivDMPSList = indivDMPSList;
    }

    public SOP getSop() {
        return sop;
    }

    public void setSop(SOP sop) {
        this.sop = sop;
    }


    public Integer getStartPeriodDate() {
        return startPeriodDate;
    }

    public void setStartPeriodDate(Integer startPeriodDate) {
        this.startPeriodDate = startPeriodDate;
    }

    public Integer getStartPeriodMonth() {
        return startPeriodMonth;
    }

    public void setStartPeriodMonth(Integer startPeriodMonth) {
        this.startPeriodMonth = startPeriodMonth;
    }

    public Integer getStartPeriodYear() {
        return startPeriodYear;
    }

    public void setStartPeriodYear(Integer startPeriodYear) {
        this.startPeriodYear = startPeriodYear;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {

        if (!(object instanceof DailyMPS)) {
            return false;
        }
        DailyMPS other = (DailyMPS) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.mrp.ppm.entity.DailyMPS[ id=" + id + " ]";
    }
}
